package com.bibrApp.difu.dialog;

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.bibrApp.difu.R;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatDialog;


/**
 * Created by dj on 2020/2/26
 */

public class MyCustomDialog extends AppCompatDialog {

    public MyCustomDialog(Context context, int theme, View view) {
        super(context, theme);
    }

    public static class Builder {
        private MyCustomDialog dialog;
        private View rootView;
        private View.OnClickListener mListener;
        private Context context;
        private int theme;
        private String title;
        private String content;
        private String rightBtnStr;
        private OnGetVerifyCodeClick onGetVerifyCodeClick;
        private OnButtonClick onButtonClick;
        private boolean titleIsVisible;
        private boolean contentIsVisible;
        private boolean etIsVisible;
        private boolean flIsVisible;
        private boolean getCodeTxInVisible;//设置是否消失，注意这里true表示消失
        private boolean twoBtnInVisible;//设置是否消失，注意这里true表示消失
        private boolean closeIvIsVisible;

        public Builder(Context context, int theme) {
            this.context = context;
            this.theme = theme;
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View view = inflater.inflate(R.layout.dialog_my_custom, null);
            dialog = new MyCustomDialog(context, theme, view);
            dialog.setCanceledOnTouchOutside(false);//点击其他区域不可关闭弹窗
            rootView = inflater.inflate(R.layout.dialog_my_custom, null, false);
            dialog.addContentView(rootView, new ViewGroup.LayoutParams((int) (ScreenUtils.getScreenDispaly(context)[0] * 0.75),
                    ViewGroup.LayoutParams.WRAP_CONTENT));
            dialog.setContentView(rootView);

            TextView sureBtn = rootView.findViewById(R.id.dialog_btn_right);
            EditText codeET = rootView.findViewById(R.id.dialog_security_code_et);
            TextView getCodeTV = rootView.findViewById(R.id.dialog_verify_code_get_code_tv);
            sureBtn.setOnClickListener(v -> {
                if (onButtonClick != null) {
                    String code = codeET.getText().toString().replaceAll(" ","");
//                    v.setTag(code);
                    onButtonClick.onOk(code);
//                    mListener.onClick(v);
                }
            });

            TextView cancelBtn = rootView.findViewById(R.id.dialog_btn_left);
            cancelBtn.setOnClickListener(v -> {
                if(onButtonClick != null){
                    onButtonClick.onCancel();
                    dismiss();
                }
            });
            getCodeTV.setOnClickListener(v -> {
                if (onGetVerifyCodeClick != null) {
                    onGetVerifyCodeClick.onClickGetVerifyCode(getCodeTV);
                }
            });
        }

        public MyCustomDialog.Builder setTitle(CharSequence character){
            TextView titleTV = rootView.findViewById(R.id.dialog_title);
            titleTV.setText(character);
            return this;
        }

        public MyCustomDialog.Builder setContent(CharSequence character){
            TextView contentTV = rootView.findViewById(R.id.dialog_content);
            contentTV.setText(character);
            return this;
        }

        public MyCustomDialog.Builder setRightBtnStr(CharSequence character){
            Button rightBtn = rootView.findViewById(R.id.dialog_btn_right);
            rightBtn.setText(character);
            return this;
        }

        public MyCustomDialog.Builder setOnGetVerifyCodeClick(OnGetVerifyCodeClick onGetVerifyCodeClick) {
            this.onGetVerifyCodeClick = onGetVerifyCodeClick;
            return this;
        }

        public MyCustomDialog.Builder setOnClickListener(@NonNull View.OnClickListener listener) {
            this.mListener = listener;
            return this;
        }

        public MyCustomDialog.Builder setOnBtnClickListener(OnButtonClick onButtonClick){
            this.onButtonClick = onButtonClick;
            return this;
        }

        /*public boolean isTwoBtnInVisible() {
            return twoBtnInVisible;
        }

        public void setTwoBtnInVisible(boolean twoBtnInVisible) {
            this.twoBtnInVisible = twoBtnInVisible;
        }

        public boolean isCloseIvIsVisible() {
            return closeIvIsVisible;
        }

        public void setCloseIvIsVisible(boolean closeIvIsVisible) {
            this.closeIvIsVisible = closeIvIsVisible;
        }

        public boolean isGetCodeTxVisible() {
            return getCodeTxInVisible;
        }

        public void setGetCodeTxVisible(boolean getCodeTxVisible) {
            this.getCodeTxInVisible = getCodeTxVisible;
        }

        public boolean isFlIsVisible() {
            return flIsVisible;
        }

        public void setFlIsVisible(boolean flIsVisible) {
            this.flIsVisible = flIsVisible;
        }

        public String getRightBtnStr() {
            return rightBtnStr;
        }

        public void setRightBtnStr(String rightBtnStr) {
            this.rightBtnStr = rightBtnStr;
        }

        public boolean isEtIsVisible() {
            return etIsVisible;
        }

        public void setEtIsVisible(boolean etIsVisible) {
            this.etIsVisible = etIsVisible;
        }

        public void setButtonClick(OnButtonClick buttonClick) {
            this.buttonClick = buttonClick;
        }


        public boolean isTitleIsVisible() {
            return titleIsVisible;
        }

        public void setTitleIsVisible(boolean titleIsVisible) {
            this.titleIsVisible = titleIsVisible;
        }

        public boolean isContentIsVisible() {
            return contentIsVisible;
        }

        public void setContentIsVisible(boolean contentIsVisible) {
            this.contentIsVisible = contentIsVisible;
        }

        public MyCustomDialog creater() {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View view = inflater.inflate(R.layout.dialog_my_custom, null);
            dialog = new MyCustomDialog(context, theme, view);
            dialog.setCanceledOnTouchOutside(false);//点击其他区域不可关闭弹窗
            Button leftBtn = view.findViewById(R.id.dialog_btn_left);
            Button rightBtn = view.findViewById(R.id.dialog_btn_right);
            TextView titletv = view.findViewById(R.id.dialog_title);
            TextView contentTV = view.findViewById(R.id.dialog_content);
            FrameLayout inputCodeFL = view.findViewById(R.id.dialog_input_verify_code_fl);
            EditText codeET = view.findViewById(R.id.dialog_security_code_et);
            TextView getCodeTV = view.findViewById(R.id.dialog_verify_code_get_code_tv);
            LinearLayout twoBtnLL = view.findViewById(R.id.two_btn_ll);
            if (twoBtnInVisible) {
                twoBtnLL.setVisibility(View.GONE);
            } else {
                twoBtnLL.setVisibility(View.VISIBLE);
            }
            if (getCodeTxInVisible) {
                getCodeTV.setVisibility(View.GONE);
            } else {
                getCodeTV.setVisibility(View.VISIBLE);
            }
            if (isFlIsVisible()) {
                inputCodeFL.setVisibility(View.VISIBLE);
            } else {
                inputCodeFL.setVisibility(View.GONE);
            }
            if (titleIsVisible) {
                titletv.setVisibility(View.VISIBLE);
            } else {
                titletv.setVisibility(View.GONE);
            }
            if (contentIsVisible) {
                contentTV.setVisibility(View.VISIBLE);
            } else {
                contentTV.setVisibility(View.GONE);
            }
            titletv.setText(title);
            contentTV.setText(content);
            rightBtn.setText(rightBtnStr);

            leftBtn.setOnClickListener(v -> {
                if (buttonClick != null) {
                    buttonClick.onCancel();
                }
                dismiss();
            });


            getCodeTV.setOnClickListener(v -> {
                if (onGetVerifyCodeClick != null && isFlIsVisible()) {
                    onGetVerifyCodeClick.onClickGetVerifyCode(getCodeTV);
                }
            });

            rightBtn.setOnClickListener(v -> {
                if (buttonClick != null && isFlIsVisible()) {
                    String codeStr = codeET.getText().toString().replaceAll(" ", "");
                    buttonClick.onOk(codeStr);
                } else {
                    buttonClick.onOk();
                }
            });
            return dialog;
        }*/


        public MyCustomDialog dismiss() {
            if (dialog != null && dialog.isShowing()) {
                dialog.dismiss();
            }
            return dialog;
        }

        public MyCustomDialog show() {
            if (dialog != null && !dialog.isShowing()) {
                dialog.show();
            }
            return dialog;
        }
    }

    public interface OnButtonClick {
        void onOk(String checkCode);

        void onOk();

        void onCancel();
    }

    public interface OnGetVerifyCodeClick {
        void onClickGetVerifyCode(TextView tv);
    }
}
